package org.me.SendSMS.utli;

import java.util.ArrayList;
import java.util.List;

import org.me.SendSMS.data.ContactDetails;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.util.Log;

public class ReadContact {
	
	public static List<ContactDetails> ReadContactDetail(Context mContext)
	{
		List<ContactDetails> list = new ArrayList<ContactDetails>();
		Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
          String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          Log.w("create", "name = "+name);
          
          String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          Log.w("create", "phoneNumber = "+phoneNumber);
          
          list.add(new ContactDetails(name,phoneNumber));
        }
        phones.close();
        
        
        
        GlobalVariable.listContact = list;
        
        
        
        return list;
	}
	
	public String getContactDisplayNameByNumber(String number, Context mContext) {
	    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
	    String name = "?";

	    ContentResolver contentResolver = mContext.getContentResolver();
	    Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
	            ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

	    try {
	        if (contactLookup != null && contactLookup.getCount() > 0) {
	            contactLookup.moveToNext();
	            name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
	            //String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
	        }
	    } finally {
	        if (contactLookup != null) {
	            contactLookup.close();
	        }
	    }

	    return name;
	}


}
